<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>新增评价</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="{{URL::asset('layui/css/layui.css')}}"  media="all">
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>

<form class="layui-form" action="">
    <fieldset class="layui-elem-field site-demo-button" style="margin-top: 20px;">
        <legend>基本信息</legend>
        <div>
            <div class="layui-form-item">
                <label class="layui-form-label">评价用户</label>
                <div class="layui-input-block">
                    <select id="classId" name="userid" lay-filter="aihao">
                        <option value="">请选择用户</option>
                    </select>
                </div>
            </div>

            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label">评价内容</label>
                <div class="layui-input-block">
                    <textarea placeholder="请输入内容" name="content" class="layui-textarea"></textarea>
                </div>
            </div>


            <div class="layui-form-item">
                <label class="layui-form-label">评价图片</label>
                <div class="layui-input-block">
                    <div class="layui-upload">
                        <input type="hidden" id="image" name="image" autocomplete="off" class="layui-input">
                        <button type="button" class="layui-btn" id="test1">选择图片</button>
                        <blockquote class="layui-elem-quote layui-quote-nm" style="margin-top: 10px;">
                            预览图:
                            <div class="layui-upload-list" id="demo1"></div>
                        </blockquote>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button type="button" class="layui-btn" lay-submit="" lay-filter="demo1">提交</button>
                    <button type="button" id="close" class="layui-btn layui-btn-primary">取消</button>
                </div>
            </div>
        </div>
    </fieldset>

</form>

<script src="{{URL::asset('layui/layui.js')}}" charset="utf-8"></script>
<script src="{{URL::asset('js/jquery-3.1.1.min.js')}}"></script>
<script>
    layui.use(['form','upload'], function(){
        var form = layui.form
            ,upload = layui.upload
            ,layer = layui.layer;

        var id = "{{$id}}";

        var index = parent.layer.getFrameIndex(window.name);

        //自定义验证规则
        form.verify({
            title: function(value){
                if(value.length < 1){
                    return '必填项不能为空';
                }
            }
        });

        //商品类型
        goodsTypeList();


        //单图片上传
        upload.render({
            elem: '#test1'
            ,url: '/admin/upload'
            ,field:'files'
            ,before: function(obj){
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    $('#demo1').append('<img src="'+ result +'" alt="'+ file.name +'" class="layui-upload-img" style="width: 80px;height: 80px;">')
                });
            }
            ,done: function(res){
                var image = $('#image').val();
                if (res.code==200){
                    var image = image+','+res.data.image;
                    $('#image').val(image);
                }
            }
        });


        //监听提交
        form.on('submit(demo1)', function(data){
            var f = data.field;
            var where = {"userid":f.userid,"goods_id":id,"content":f.content,"image":(f.image).substr(1)};
            var url = "/admin/goods/ping/api/add";
            $.post(url,where,function (d) {
                if (d.code==200){
                    layer.msg("评价新增成功!");
                    setTimeout(function () {
                        window.parent.location.reload();
                    },500)
                }else{
                    layer.msg("评价新增失败!");
                }
            });

            return false;
        });

        function goodsTypeList() {
            var url = "/admin/member/api/admin/list";
            $.get(url,function (d) {
                if (d.code==200){
                    $.each(d.data,function (i,v) {
                        var jd = '<option value="'+v.id+'">'+v.nickname+'</option>';
                        $('#classId').append(jd);
                    });
                }
                form.render('select');
            });
        }

        $('#close').click(function(){
            parent.layer.close(index);
        });
    });
</script>

</body>
</html>